package cn.yunhe.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import cn.yunhe.biz.IUniverstityBiz;
import cn.yunhe.biz.UniverstityBizImpl;
import cn.yunhe.entity.University;

/**
 * Servlet implementation class DoUser
 */
public class DoUniversity extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DoUniversity() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: ").append(request.getContextPath());
		HttpSession session = request.getSession();
		PrintWriter out =response.getWriter();
		IUniverstityBiz iub = new UniverstityBizImpl();
		
		String opt = request.getParameter("opt");
		if("add".equals(opt)){
			String uname = request.getParameter("uname");
			String sisnef = request.getParameter("isnef");
			String sistoo = request.getParameter("istoo");
			String contacts = request.getParameter("contacts");
			String url = request.getParameter("url");
			
			int isnef = Integer.parseInt(sisnef);
			int istoo = Integer.parseInt(sistoo);
			
			
			University u = new University();
			u.setUname(uname);
			u.setIsnef(isnef);
			u.setContacts(contacts);
			u.setIstoo(istoo);
			u.setUrl(url);
			
			boolean r = iub.add(u);
			response.sendRedirect("DoBackstage?opt=getUniversity");
		}else if("update".equals(opt)){
			String suniid = request.getParameter("universityid");
			String uname = request.getParameter("uname");
			String sisnef = request.getParameter("isnef");
			String sistoo = request.getParameter("istoo");
			String contacts = request.getParameter("contacts");
			String url = request.getParameter("url");
			int isnef = Integer.parseInt(sisnef);
			int istoo = Integer.parseInt(sistoo);
			Integer universityid = Integer.parseInt(suniid);
			University u = new University();
			u.setUniversityid(universityid);
			u.setUname(uname);
			u.setIsnef(isnef);
			u.setContacts(contacts);
			u.setIstoo(istoo);
			u.setUrl(url);
			
			boolean r= iub.updateUniversity(u);
			response.sendRedirect("DoBackstage?opt=getUniversity");
		}else if("del".equals(opt)){
			String suniid = request.getParameter("universityid");
			int universityid = Integer.parseInt(suniid);
			
			boolean r = iub.delUniversity(universityid);
			response.sendRedirect("DoBackstage?opt=getUniversity");
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
